/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source 
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.  
 */

package jdbcmanager.tree;

import java.sql.*;

import java.awt.*;
import javax.swing.*;

import jdbcmanager.app.*;
import jdbcmanager.dbtools.*;
import jdbcmanager.dbtools.datatypes.*;
import jdbcmanager.swing.*;
import jdbcmanager.swing.table.*;

/**
 * 
 * @author Francisco Morero Peyrona
 */

final class Info4Table extends JTabbedPane
{
   private DbEntityDescriptor table;
   private JTextNote txtError = new JTextNote();
   
   Info4Table( DbEntityDescriptor table )
   {
      this.table = table;
            
      this.txtError.setBorder( BorderFactory.createEmptyBorder( 30, 20, 0, 20 ) );
      this.txtError.setText( "Error retrieveing this information.\n"+
                             "\n"+ 
                             "Most probably this is not an error in the application or in\n"+
                             "communciating with selected DBMS.\n" +
                             "It will be simply that the JDBC driver does not provide the information." );
      
      add( getColumns()      , "Columns" );
      add( getPrimaryKey()   , "Primary Key" );
      add( getIndices()      , "Indexes" );
      add( getRowIDs()       , "Row IDs" );
      add( getImportedKeys() , "Imported Keys" );
      add( getExportedKeys() , "Exported Keys" );
      add( getPrivileges()   , "Privileges" );
      add( getColPrivileges(), "Column Privileges" );
      add( getVersions()     , "Versions" );
      add( getInfo()         , "Info" );
   }
   
   private JPanel getColumns()
   {
      JPanel panel = new JPanel( new BorderLayout() );
      
      try
      {
         ResultSet rs = DbUtil.getAgentDBMS().getAgentMetaData().
                        getColumns( this.table.getCatalog(), this.table.getSchema(), this.table.getName() );
         JxTableRamRowSet table = new JxTableRamRowSet( rs );
         
         panel.add( new JScrollPane( table ), BorderLayout.CENTER );
      }
      catch( Exception exc )
      {
         App.showDebugException( exc );
         panel.add( this.txtError, BorderLayout.CENTER );
      }
      
      return panel;
   }
   
   private JPanel getPrimaryKey()
   {
      JPanel panel = new JPanel( new BorderLayout() );
      
      try
      {
         ResultSet rs = DbUtil.getAgentDBMS().getAgentMetaData().
                        getPrimaryKeys( this.table.getCatalog(), this.table.getSchema(), this.table.getName() );
         JxTableRamRowSet table = new JxTableRamRowSet( rs );
   
         panel.add( new JScrollPane( table ), BorderLayout.CENTER );
      }
      catch( Exception exc )
      {
         App.showDebugException( exc );
         panel.add( this.txtError, BorderLayout.CENTER );
      }

      return panel;
   }
   
   private JPanel getImportedKeys()
   {
      JPanel panel = new JPanel( new BorderLayout() );
      
      try
      {
         ResultSet rs = DbUtil.getAgentDBMS().getAgentMetaData().
                        getImportedKeys( this.table.getCatalog(), this.table.getSchema(), this.table.getName() );
         JxTableRamRowSet table = new JxTableRamRowSet( rs );
   
         panel.add( new JScrollPane( table ), BorderLayout.CENTER );
      }
      catch( Exception exc )
      {
         App.showDebugException( exc );
         panel.add( this.txtError, BorderLayout.CENTER );
      }

      return panel;      
   }
   
   private JPanel getExportedKeys()
   {
      JPanel panel = new JPanel( new BorderLayout() );
      
      try
      {
         ResultSet rs = DbUtil.getAgentDBMS().getAgentMetaData().
                        getExportedKeys( this.table.getCatalog(), this.table.getSchema(), this.table.getName() );
         JxTableRamRowSet table = new JxTableRamRowSet( rs );
   
         panel.add( new JScrollPane( table ), BorderLayout.CENTER );
      }
      catch( Exception exc )
      {
         App.showDebugException( exc );
         panel.add( this.txtError, BorderLayout.CENTER );
      }

      return panel;
   }

   private JPanel getIndices()
   {
      JPanel panel = new JPanel( new BorderLayout() );
      
      try
      {
         ResultSet rs = DbUtil.getAgentDBMS().getAgentMetaData().
                        getIndices( this.table.getCatalog(), this.table.getSchema(), this.table.getName() );
         JxTableRamRowSet table = new JxTableRamRowSet( rs );

         panel.add( new JScrollPane( table ), BorderLayout.CENTER );
      }
      catch( Exception exc )
      {
         App.showDebugException( exc );
         panel.add( this.txtError, BorderLayout.CENTER );
      }

      return panel;      
   }

   private JPanel getRowIDs()
   {
      JPanel panel = new JPanel( new BorderLayout() );
      
      try
      {
         ResultSet rs = DbUtil.getAgentDBMS().getAgentMetaData().
                        getRowIDs( this.table.getCatalog(), this.table.getSchema(), this.table.getName() );
         JxTableRamRowSet table = new JxTableRamRowSet( rs );

         panel.add( new JScrollPane( table ), BorderLayout.CENTER );
      }
      catch( Exception exc )
      {
         App.showDebugException( exc );
         panel.add( this.txtError, BorderLayout.CENTER );
      }

      return panel;           
   }

   private JPanel getPrivileges()
   {
      JPanel panel = new JPanel( new BorderLayout() );
      
      try
      {
         ResultSet rs = DbUtil.getAgentDBMS().getAgentMetaData().
                        getTablePrivileges( this.table.getCatalog(), this.table.getSchema(), this.table.getName() );
         JxTableRamRowSet table = new JxTableRamRowSet( rs );

         panel.add( new JScrollPane( table ), BorderLayout.CENTER );
      }
      catch( Exception exc )
      {
         App.showDebugException( exc );
         panel.add( this.txtError, BorderLayout.CENTER );
      }

      return panel;                
   }

   private JPanel getColPrivileges()
   {
      JPanel panel = new JPanel( new BorderLayout() );
      
      try
      {
         ResultSet rs = DbUtil.getAgentDBMS().getAgentMetaData().
                        getColumnPrivileges( this.table.getCatalog(), this.table.getSchema(), this.table.getName() );
         JxTableRamRowSet table = new JxTableRamRowSet( rs );

         panel.add( new JScrollPane( table ), BorderLayout.CENTER );
      }
      catch( Exception exc )
      {
         App.showDebugException( exc );
         panel.add( this.txtError, BorderLayout.CENTER );
      }

      return panel;                
   }

   private JPanel getVersions()
   {
      JPanel panel = new JPanel( new BorderLayout() );
      
      try
      {
         ResultSet rs = DbUtil.getAgentDBMS().getAgentMetaData().
                        getVersions( this.table.getCatalog(), this.table.getSchema(), this.table.getName() );
         JxTableRamRowSet table = new JxTableRamRowSet( rs );

         panel.add( new JScrollPane( table ), BorderLayout.CENTER );
      }
      catch( Exception exc )
      {
         App.showDebugException( exc );
         panel.add( this.txtError, BorderLayout.CENTER );
      }

      return panel;                
   }

   private JPanel getInfo()
   {
      JPanel panel = new JPanel( new BorderLayout() );
      
      try
      {
         JxTableMap table = new JxTableMap();
                    table.addRow( "Catalog"                                               , this.table.getCatalog() );
                    table.addRow( DbUtil.getAgentDBMS().getAgentMetaData().getSchemaTerm(), this.table.getSchema() );
                    table.addRow( "Type"                                                  , this.table.getType() );
                    table.addRow( "Simple name"                                           , this.table.getName() );
                    table.addRow( "Qualified Name"                                        , this.table.getQualifiedName() );
                    table.adjustColumnsWidth();
                    
         panel.add( new JScrollPane( table ), BorderLayout.CENTER );
      }
      catch( Exception exc )
      {
         App.showDebugException( exc );
         panel.add( this.txtError, BorderLayout.CENTER );
      }

      return panel;                
   }
}